
#include <BFont>
#include <BStyle>
#include <BPainter>

#include "TextBoard.h"

TextBoard::TextBoard(BWidget* parent) : BTextEdit(parent)
{
	_dirty = true;
	this->setMargin(3);
}
TextBoard::~TextBoard()
{

}

void TextBoard::styleEvent(const BEvent& event)
{
	BTextEdit::styleEvent(event);
	const BStyle* style = event.value();
}
void TextBoard::freshEvent(const BEvent& event)
{
	BTextEdit::freshEvent(event);
	if (_dirty)
	{
		int left = 20;
		BString text = this->lineCount();
		if (BFont* font = this->font())
		{
			left = font->textSize(text).width() + 3;
		}
		this->setPerch(Part_Left, left);
		_dirty = false;
	}
}
void TextBoard::paintEvent(const BEvent& event)
{
	BTextEdit::paintEvent(event);

	BRect leftRect = this->rect(Part_Left);
	BPainter painter(this);
	painter.setColor(Color_Text);
	BString lineText;
	const BIntArray& lines = this->query("visual-lines");
	for (int i = 0; i < lines.size(); i++)
	{
		int line = lines[i];
		if (!this->lineVisible(line))
			continue;
		BRect lineRect = this->lineRect(line);
		lineRect.left() = leftRect.left();
		lineRect.right() = leftRect.right();
		lineText.reset();
		lineText << line + 1;
		painter.drawText(lineRect, lineText, Align_RightCenter);
	}
}
